package com.kuhyygy;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.kuhyygy.activity.AsyncActivity;
import com.kuhyygy.activity.BaseActivity;
import com.kuhyygy.activity.BroadCastActivity;
import com.kuhyygy.activity.ChatActivity;
import com.kuhyygy.activity.CommonUIActivity;
import com.kuhyygy.activity.ContentProviderActivity;
import com.kuhyygy.activity.DbActivity;
import com.kuhyygy.activity.DialogActivity;
import com.kuhyygy.activity.EmotionActivity;
import com.kuhyygy.activity.LinkTextActivity;
import com.kuhyygy.activity.ListViewActivity;
import com.kuhyygy.activity.LoadingActivity;
import com.kuhyygy.activity.LocalBroadCastActivity;
import com.kuhyygy.activity.LoginActivity;
import com.kuhyygy.activity.ProperActivity;
import com.kuhyygy.activity.ServiceActivity;
import com.kuhyygy.uitl.ActivityCollector;
import com.kuhyygy.widget.LifeTabOldWidget;
import com.kuhyygy.widget.LifeTabWidget;
import com.kuhyygy.widget.SpannerActivity;
import com.kuhyygy.widget.autocomplate.AutocomplateActivity;

public class MainActivity extends BaseActivity {

	protected static final String TAG = "MainActivity";
	private Button serviceBtn;
	protected String action = "android.hardware.action.NEW_PICTURE";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);	// 去掉title
		
		setContentView(R.layout.activity_main);
		serviceBtn = (Button) findViewById(R.id.loadServices);
		serviceBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				Intent i = new Intent(MainActivity.this, ServiceActivity.class);
				startActivity(i);
			}
		});
		
		Button broadBtn = (Button) findViewById(R.id.broadcast);
		broadBtn.setOnClickListener(lister);
		
		Button lbBtn = (Button) findViewById(R.id.localBCBtn);
		lbBtn.setOnClickListener(lister);
		
		Button rwPropBtn = (Button) findViewById(R.id.rwProp);
		rwPropBtn.setOnClickListener(lister);
		
		Button cpBtn = (Button) findViewById(R.id.cpBtn);
		cpBtn.setOnClickListener(lister);
		
		Button listViewBtn = (Button) findViewById(R.id.listViewBtn);
		listViewBtn.setOnClickListener(lister);
		
		Button tabBtn = (Button) findViewById(R.id.tabBtnOld);
		tabBtn.setOnClickListener(lister);
		Button tabBtnnew = (Button) findViewById(R.id.tabBtnNew);
		tabBtnnew.setOnClickListener(lister);
		
		Button autoCBtn = (Button) findViewById(R.id.autoBtn);
		autoCBtn.setOnClickListener(lister);
		
		Button spinBtn = (Button) findViewById(R.id.spinnerBtn);
		spinBtn.setOnClickListener(lister);
		
		Button diaBtn = (Button) findViewById(R.id.diaBtn);
		diaBtn.setOnClickListener(lister);
		
		Button loadmBtn = (Button) findViewById(R.id.loadMoreBtn);
		loadmBtn.setOnClickListener(lister);
		
		Button asyncBtn = (Button) findViewById(R.id.asyncBtn);
		asyncBtn.setOnClickListener(lister);
		
		Button textLinkBtn = (Button) findViewById(R.id.textLinkBtn);
		textLinkBtn.setOnClickListener(lister);
		
		Button commonUIBtn = (Button) findViewById(R.id.commonUI);
		commonUIBtn.setOnClickListener(lister);
		
		Button refLvBtn = (Button) findViewById(R.id.freshListView);
		refLvBtn.setOnClickListener(lister);
		
		Button chatWinBtn = (Button) findViewById(R.id.chatWinView);
		chatWinBtn.setOnClickListener(lister);
		
		Button frcOffBtn = (Button) findViewById(R.id.forceOffLineBtn);
		frcOffBtn.setOnClickListener(lister);
		
		Button dbBtn = (Button) findViewById(R.id.dbBtn);
		dbBtn.setOnClickListener(lister);
	}
	
	private OnClickListener lister = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.broadcast:
				Intent i = new Intent(MainActivity.this, BroadCastActivity.class);
				startActivity(i);
				break;
			case R.id.localBCBtn:
				Intent lobc = new Intent(MainActivity.this, LocalBroadCastActivity.class);
				startActivity(lobc);
				break;
			case R.id.rwProp:
				Intent it = new Intent(MainActivity.this, ProperActivity.class);
				startActivity(it);
				break;
			case R.id.cpBtn:
				Intent cpIt = new Intent(MainActivity.this, ContentProviderActivity.class);
				startActivity(cpIt);
				break;
			case R.id.listViewBtn:
				startActivity(new Intent(MainActivity.this, ListViewActivity.class));
				break;
			case R.id.tabBtnOld:
				startActivity(new Intent(MainActivity.this, LifeTabOldWidget.class));
				break;
			case R.id.tabBtnNew:
				startActivity(new Intent(MainActivity.this, LifeTabWidget.class));
				break;
			case R.id.autoBtn:
				startActivity(new Intent(MainActivity.this, AutocomplateActivity.class));
				break;
			case R.id.spinnerBtn:
				startActivity(new Intent(MainActivity.this, SpannerActivity.class));
				break;
			case R.id.diaBtn:
				startActivity(new Intent(MainActivity.this, DialogActivity.class));
				break;
			case R.id.loadMoreBtn:
				startActivity(new Intent(MainActivity.this, LoadingActivity.class));
				break;
			case R.id.asyncBtn:
				startActivity(new Intent(MainActivity.this, AsyncActivity.class));
				break;
			case R.id.textLinkBtn:
				startActivity(new Intent(MainActivity.this, LinkTextActivity.class));
				break;
			case R.id.commonUI:
				startActivity(new Intent(MainActivity.this, CommonUIActivity.class));
				break;
			case R.id.freshListView:
				startActivity(new Intent(MainActivity.this, EmotionActivity.class));
				break;
			case R.id.chatWinView:
				startActivity(new Intent(MainActivity.this, ChatActivity.class));
				break;
			case R.id.forceOffLineBtn:
				startActivity(new Intent(MainActivity.this, LoginActivity.class));
				break;
			case R.id.dbBtn:
				startActivity(new Intent(MainActivity.this, DbActivity.class));
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 菜单加载
	 */
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	};
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.personalSetting:
			alert(this.getResources().getString(R.string.personalSetting));
			break;
		case R.id.exit:
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
			alertDialog.setTitle("确认要退出程序?");
			alertDialog.setCancelable(false);
			alertDialog.setPositiveButton("是", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					ActivityCollector.removeAll();
				}
			});
			alertDialog.setNegativeButton("否", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					ProgressDialog pd = new ProgressDialog(MainActivity.this);
					pd.setTitle("退出");
					pd.setMessage("exiting ...");
					pd.setCancelable(true);
					pd.show();
				}
			});
			alertDialog.show();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("saveInfo", "之前保存的数据了吗");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	private void alert(Object o) {
		Toast.makeText(this, o.toString(), Toast.LENGTH_SHORT).show();
	}
}
